package com.taxshare.tools.util;


import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * @Auther: luozy
 * @Date: 2018/11/21 09:26
 * @Description:
 */
public class DoubleUtils {
    public static final DecimalFormat TWO_BIT = new DecimalFormat("######0.00");

    public static String doubleFormat(double d, DecimalFormat format) {
        return format.format(d);
    }

    public static String doubleFormatStr(double d) {
        return TWO_BIT.format(d);
    }


    public static double doubleFormatDou(double d) {
        return Double.parseDouble(doubleFormatStr(d));
    }

    public static double stringFormatdouble(String d) {
        if (!StringUtil.isEmpty(d)) {
            return Double.parseDouble(d);
        }
        return 0;
    }

    public static String doubleFormatDec(String d) {
        double a = 0.0;
        if (!StringUtil.isEmpty(d)) {
            a = Double.parseDouble(d);
        }
        return TWO_BIT.format(a);
    }

    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double addStr(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(doubleFormatDec(v1));
        BigDecimal b2 = new BigDecimal(doubleFormatDec(v2));
        return b1.add(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static String mulStr(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(doubleFormatDec(v1));
        BigDecimal b2 = new BigDecimal(doubleFormatDec(v2));
        return b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String divStr(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(doubleFormatDec(v1));
        BigDecimal b2 = new BigDecimal(doubleFormatDec(v2));
        return b1.divide(b2).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String subStr(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(doubleFormatDec(v1));
        BigDecimal b2 = new BigDecimal(doubleFormatDec(v2));
        return b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static double sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}